@echo off
echo Starting YanXue AI Go Microservices with custom configuration...
echo.

REM 检查Go环境
go version >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: Go is not installed or not in PATH
    pause
    exit /b 1
)

REM 检查配置文件
if not exist "configs\config.yml" (
    echo Error: config.yml not found in configs directory
    pause
    exit /b 1
)

REM 设置环境变量（基于您提供的配置）
set APP_NAME=example-service
set APP_VERSION=1.0.0
set APP_ENVIRONMENT=development
set SERVER_HOST=0.0.0.0
set SERVER_PORT=8080
set DATABASE_HOST=127.0.0.1
set DATABASE_PORT=3306
set DATABASE_USER=root
set DATABASE_PASSWORD=password
set DATABASE_NAME=yanxue_ai
set REDIS_HOST=127.0.0.1
set REDIS_PORT=6379
set REDIS_PASSWORD=redis_password
set AI_OPENAI_API_KEY=your_api_key_here
set AI_OPENAI_BASE_URL=https://api.example.com
set LOG_LEVEL=info

echo Configuration loaded for %APP_NAME% v%APP_VERSION%
echo Environment: %APP_ENVIRONMENT%
echo Server will start on %SERVER_HOST%:%SERVER_PORT%
echo Database: %DATABASE_HOST%:%DATABASE_PORT%/%DATABASE_NAME%
echo Redis: %REDIS_HOST%:%REDIS_PORT%
echo AI Endpoint: %AI_OPENAI_BASE_URL%
echo.

REM 检查依赖
echo Checking and installing dependencies...
go mod tidy
if %errorlevel% neq 0 (
    echo Error: Failed to install dependencies
    pause
    exit /b 1
)

REM 创建bin目录
if not exist "bin" (
    mkdir bin
)

REM 构建所有服务
echo Building all services...
echo.

echo Building Gateway service...
cd services\gateway
go build -ldflags="-w -s" -o ..\..\bin\gateway.exe .\cmd\main.go
if %errorlevel% neq 0 (
    echo Error: Failed to build gateway service
    cd ..\..
    pause
    exit /b 1
)
cd ..\..

echo Building Agent service...
cd services\agent
go build -ldflags="-w -s" -o ..\..\bin\agent.exe .\cmd\main.go
if %errorlevel% neq 0 (
    echo Error: Failed to build agent service
    cd ..\..
    pause
    exit /b 1
)
cd ..\..

echo Building MCP service...
cd services\mcp
go build -ldflags="-w -s" -o ..\..\bin\mcp.exe .\cmd\main.go
if %errorlevel% neq 0 (
    echo Error: Failed to build mcp service
    cd ..\..
    pause
    exit /b 1
)
cd ..\..

echo Building RAG service...
cd services\rag
go build -ldflags="-w -s" -o ..\..\bin\rag.exe .\cmd\main.go
if %errorlevel% neq 0 (
    echo Error: Failed to build rag service
    cd ..\..
    pause
    exit /b 1
)
cd ..\..

echo.
echo All services built successfully!
echo.
echo Starting services...
echo.
echo Note: This will start the Gateway service on port %SERVER_PORT%
echo Other services will start on their configured ports
echo Press Ctrl+C to stop all services
echo.

REM 启动网关服务（主要入口）
echo Starting Gateway service on %SERVER_HOST%:%SERVER_PORT%...
.\bin\gateway.exe

pause